<?php

namespace app\controller;

use app\service\TaskStatisticsService;
use app\service\UserStatisticsService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class TaskStatisticsController
{
    /**
     * @var TaskStatisticsService
     */
    private $taskStatisticsService;
    private $taskSliceLimit;
    private $taskStatisticsStructure;

    public function __construct()
    {
        $this->taskStatisticsService = TaskStatisticsService::getInstance();

        // 参数task_id列表长度限制
        $this->taskSliceLimit = 1000;
        $this->taskStatisticsStructure = [
            'project_id' => 'split_array|array_map,intval',                 // 项目ID 列表
            'task_id' => 'split_array|array_map,intval|array_slice,{{task_id}},0,' . $this->taskSliceLimit,    // 支持任务列表查询
            'step_category_id' => 'split_array|array_map,intval',           // 环节ID 列表
            'department_id' => 'split_array|array_map,intval',              // 部门ID 列表
            'start_time' => 'intval',
            'end_time' => 'intval',
            'query_code' => 'split_array|array_map,strval|array_unique',    // 计划、计划内完成、计划外完成、未完成、未处理、已通过、逾期、延期
            'query_category' => 'strval',                                   // 任务类型过滤: 生产、标准、
            'task_step_type' => 'split_array|array_map,strval|array_unique', // 指定任务类型
            'task_status' => 'filter_extract|split_array|array_map,strval|array_unique', // 任务状态过滤
            'query_type' => 'strval',                                       // 快速统计、完全统计
            'view_type' => 'strval',                                        // 单统计、多日统计
            'history' => 'strval',                                          // 是否历史
            'node_mode' => 'strval',                                             // 是否节点(不受时间限制，不受无法追踪的变更之影响)
        ];
    }

    /**
     * 通过 任务状态、完成时间 查询的统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function taskDoneStatistics(Request $request): Response
    {
        $param = query_array_construct($this->taskStatisticsStructure, $request->post());
        $res = $this->taskStatisticsService->taskStatistics($request, $param, 'done');
        return json(success_response('', $res));
    }

    /**
     * 通过 任务状态、交付时间 查询的统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function taskDeliveryStatistics(Request $request): Response
    {
        $param = query_array_construct($this->taskStatisticsStructure, $request->post());
        $res = $this->taskStatisticsService->taskStatistics($request, $param, 'delivery');
        return json(success_response('', $res));
    }

    /**
     * 任务统计 - 综合
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function taskAbnormalStatistics(Request $request): Response
    {
        $param = query_array_construct($this->taskStatisticsStructure, $request->post());
        $res = $this->taskStatisticsService->taskStatistics($request, $param, 'abnormal');
        return json(success_response('', $res));
    }

    /**
     * 任务统计 - 综合
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function taskStatistics(Request $request): Response
    {
        $param = query_array_construct($this->taskStatisticsStructure, $request->post());
        $res = $this->taskStatisticsService->taskStatistics($request, $param, 'dynamic');
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return Response
     */
    public function userRoleTaskCount(Request $request): Response
    {
        $param = query_array_construct([
            'project_id' => 'intval',                 // 项目ID 列表
            'role_code' => 'strval',                                        // 用户角色code
            'start_time' => 'intval',
            'end_time' => 'intval',
        ], $request->post());

        $res = UserStatisticsService::getInstance()->userRoleTaskCount($param);
        return json(success_response('', $res));
    }
}